package org.mx.test.graphql;

import graphql.schema.DataFetchingEnvironment;
import org.mx.service.rest.graphql.GraphQLAnnotationExecution;
import org.mx.service.rest.graphql.GraphQLFieldAnnotation;
import org.mx.test.service.RbacService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@SuppressWarnings("javadoc")
@Service("rbacMutationExecution")
public class RbacMutationExecution implements GraphQLAnnotationExecution {
    private RbacService rbacService;

    @Autowired
    public RbacMutationExecution(RbacService rbacService) {
        super();
        this.rbacService = rbacService;
    }

    @GraphQLFieldAnnotation("initRbacData")
    public boolean initRbacData(DataFetchingEnvironment env) {
        rbacService.initRbacData();
        return true;
    }

    @GraphQLFieldAnnotation("cleanCacheByUserCode")
    public boolean cleanCacheByUserCode(DataFetchingEnvironment env) {
        String userCode = env.getArgument("userCode");
        rbacService.cleanCache(userCode);
        return true;
    }

    @GraphQLFieldAnnotation("cleanCache")
    public boolean cleanCache(DataFetchingEnvironment env) {
        rbacService.cleanCache();;
        return true;
    }

    @Override
    public String getTypeName() {
        return "Mutation";
    }
}
